/**
 * 入参
 * 组合ID fundCode
 * 时间戳 timeStamp
 *
 * 出参
 * 通过onRef提供getMoneyInfo方法，返回结构体{T0:xxx, T1:xxx}
 *
 */

import React, { PureComponent } from 'react';
import { Form } from '@ant-design/compatible';
import '@ant-design/compatible/assets/index.css';
import { Row, Col, Spin } from 'antd';
import { fixedNum } from '../../../utils/utils';
import { connect } from 'dva';

@Form.create()
@connect(({ depositOrderM, userM: user, loading }) => ({
  depositOrderM,
  user,
  loading,
}))
export default class CurrentAsset extends PureComponent {
  constructor(props) {
    super(props);
    this.state = {
      fundCode: undefined, //组合id
      timeStamp: undefined, //时间戳
      assetObj: {},
    };
  }

  componentDidMount() {
    this.props.onRef(this);
  }
  UNSAFE_componentWillReceiveProps(nextProps) {
    //组合ID和时间戳变更时，重新调用接口，获取当前T+0和T+1的资金
    if (nextProps.fundCode !== this.state.fundCode || nextProps.timeStamp !== this.state.timeStamp) {
      this.setState(
        {
          fundCode: nextProps.fundCode,
          timeStamp: nextProps.timeStamp,
        },
        () => {
          if (this.state.fundCode) {
            this.queryFund();
          } else {
            this.setState({
              assetObj: {},
            });
          }
        },
      );
    }
  }

  //获取当前资金
  queryFund = () => {
    this.props.dispatch({
      type: 'depositOrderM/queryFund',
      payload: {
        nodeId: this.state.fundCode,
        code: 'kk',
      },
      callback: (msg, data) => {
        if (msg == 'success') {
          this.setState({
            assetObj: {
              T0: data.tzeroAvailableFund,
              T1: data.toneAvailableFund,
            },
          });
        } else {
          this.setState({
            assetObj: {},
          });
        }
      },
    });
  };

  render() {
    const { assetObj } = this.state;

    return (
      <div className={'currentAsset'}>
        <Spin spinning={this.props.loading.effects[`depositOrderM/queryFund`] == true}>
          <Row>
            <Col span={12}>
              {' '}
              {`T+0可用:`}&nbsp;&nbsp; {assetObj.T0 !== undefined ? fixedNum(assetObj.T0, 2) : '--'}{' '}
            </Col>
            <Col span={12}>
              {' '}
              {`T+1可用:`}&nbsp;&nbsp; {assetObj.T1 !== undefined ? fixedNum(assetObj.T1, 2) : '--'}{' '}
            </Col>
          </Row>
        </Spin>
      </div>
    );
  }
}
